<?php
    $data = file_get_contents('php://input');
    $arr = array();
    if($data){
        include './config.php';
        $d = json_decode($data);
        // ip
        $ip = get_ip()->result->ip;
        $city = get_ip()->result->ad_info->province;
        // ip
        
        $timer = $d->timer;//停留时间
        $href = $d->href;//当前地址
        $title = $d->title;//当前标题
        $source = get_source($d->source);//来源地址
        $os = get_browser_info()['os'];//设备
        $bro = get_browser_info()['bro'];//浏览器
        $info = $_SERVER['HTTP_USER_AGENT'].'--'.json_encode(get_ip());//详细用户设备信息
        
        $sql_1 = "SELECT * FROM user_log WHERE source='$source' AND ip='$ip' AND href='$href' AND to_days(times) = to_days(now())";
        $stmt = $conn->query($sql_1);
        $stmt->execute();
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $result = $stmt->fetchAll();
        if($result){
            $data_id = $result[0]['Id'];
            $sql_2 = "UPDATE user_log SET num=(num+1) WHERE id='$data_id'";
            $stmt_2 = $conn->prepare($sql_2);
            $stmt_2->execute();
            $arr['code'] = 200;
            $arr['msg'] = "嘤嘤嘤~~~";
            // $arr['data'] = $info;
            echo json_encode($arr);
        }else{
            if($ip){
                $sql_3 = "INSERT INTO user_log(timer,href,title,source,os,bro,info,ip,city) VALUES('$timer','$href','$title','$source','$os','$bro','$info','$ip','$city')";
                $stmt_3 = $conn->prepare($sql_3);
                $stmt_3->execute();
                $arr['code'] = 200;
                $arr['msg'] = "欢迎~~~";
                // $arr['data'] = $info;
                echo json_encode($arr);
            };
        };
    }else{
        $arr['code'] = 400;
        $arr['msg'] = "缺少参数";
        $arr['data'] = '';
        echo json_encode($arr);
    };

    function get_browser_info(){
        $sys = array();
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MQQBrowser')){
            $sys['bro'] = '微信';
            $sys['os'] = get_os();
            return $sys;
        }
        if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==false) ){
            $sys['bro'] = 'IE';
            $sys['os'] = get_os();
            return $sys;
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
            $sys['bro'] = 'Edge';
            $sys['os'] = get_os();
            return $sys;
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
            $sys['bro'] = '火狐';
            $sys['os'] = get_os();
            return $sys;
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
            $sys['bro'] = '谷歌';
            $sys['os'] = get_os();
            return $sys;
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
            $sys['bro'] = '苹果';
            $sys['os'] = get_os();
            return $sys;
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
            $sys['bro'] = '欧朋';
            $sys['os'] = get_os();
            return $sys;
        }
         $sys['bro'] = '其他';
         $sys['os'] = get_os();
         return $sys;
       };


    function get_os(){
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Android')){
            return '安卓';
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Windows')){
            return '微软';
        }
        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'iPhone') || false!==strpos($_SERVER['HTTP_USER_AGENT'],'Macintosh')){
            return '苹果';
        }
        return '其他';
    };

    function get_source($obj){
        if(false!==strpos($obj,'baidu')){
            return '百度';
        };
        if(false!==strpos($obj,'sougou')){
            return '搜狗';
        };
        if(false!==strpos($obj,'google')){
            return '谷歌';
        };
        if(false!==strpos($obj,'mail')){
            return '邮箱';
        };
        if(!$obj || false!==strpos($obj,'aijiumai.cn')){
            return '直接访问';
        };
        return $obj;
    };
       

/*
 * 获取到当前主机IP
 */
function get_real_ip()
{
    $ip = FALSE;
    //客户端IP 或 NONE
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    //多重代理服务器下的客户端真实IP地址（可能伪造）,如果没有使用代理，此字段为空
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi("^(10│172.16│192.168).", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    //客户端IP 或 (最后一个)代理服务器 IP
    return $ip ? $ip : $_SERVER['REMOTE_ADDR'];
};
function get_ip(){
    $ip_data = file_get_contents('https://apis.map.qq.com/ws/location/v1/ip?ip='.get_real_ip().'&key=你的key');
    return json_decode($ip_data);
};

$conn = null;

